package com.example.demo.validation;

import com.example.demo.annotation.DateTime;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.text.SimpleDateFormat;

/**
 * Created by lyc on 2019/5/31.
 */
public class DateTimeConstraintValidator implements ConstraintValidator<DateTime, String> {


   @Override
   public boolean isValid(String obj, ConstraintValidatorContext context) {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      boolean flag = (obj == null);
      try {
         simpleDateFormat.parse(obj);
      }catch (Exception e){
         return false;
      }
      return !flag;
   }
}
